package com.yuchen.ishop.seller;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.MyPager;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Goods;
import com.yuchen.ishop.entity.Seller;
import com.yuchen.ishop.entity.SellerXiaoer;
import com.yuchen.ishop.entity.Shop;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.security.PasswordUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Map;


/**
 *
 */
@Transactional
@Path("sellerService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class SellerServiceImpl implements SellerService {

    private final static Logger LOG = Logger.getLogger(SellerService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("get/{id}")
    @Transactional(readOnly = true)
    public Seller getById(@PathParam("id") Long id) {
        return dao.fetch(Seller.class, id);
    }


    @POST
    @Path("insert")
    public Seller insert(Seller seller) {
        return dao.insert(seller);
    }

    @POST
    @Path("update")
    public Responses update(Seller seller) {
        dao.updateIgnoreNull(seller);
        return ResponseUtils.success();
    }

    @POST
    @Path("updatePwd")
    public Responses updatePwd(Map map) {
        if (map != null && map.size() == 2 && map.get("id")!=null && map.get("password")!=null) {
            String password = (String) map.get("password");
            Map m = PasswordUtils.genMD5Str(password);
            Seller seller = new Seller();
            seller.setId(Long.valueOf(map.get("id").toString()));
            seller.setSalt((String) m.get("salt"));
            seller.setPassword((String) m.get("password"));
            dao.updateIgnoreNull(seller);
            return ResponseUtils.success();
        }
        return ResponseUtils.error();
    }


}
